#ifndef __APPLICATIONUPDATER__
#define __APPLICATIONUPDATER__

#include <http\RHTTPTransaction.h>
#include <http\RHTTPSession.h>
#include <http\RHTTPHeaders.h>
#include <http\MHTTPTransactionCallback.h>
#include <http\MHTTPDataSupplier.h>
#include <apgcli.h>
#include <eikprogi.h>
#include <AknQueryDialog.h>
#include <aknnotewrappers.h> 
#include <AknProgressDialog.h>
#include <AknWaitDialog.h>
#include "CActiveHelper.h"

#define APPLICATION_UPDATER_RESOURCES R_APPLICATION_UPDATER_WAIT_NOTE, R_APPLICATION_UPDATER_INSTALL_QUERY, R_APPLICATION_UPDATER_PROGRESS_NOTE

enum TApplicationUpdaterEvent { EAUE_EXIT, EAUE_MANIFEST_DOWNLOADED, EAUE_CONNECTION_ERROR };
struct MApplicationUpdaterCallback
	{
	virtual void MApplicationUpdaterCallbackRunL(const TApplicationUpdaterEvent& aEvent) = 0;
	};
class CApplicationUpdater : public MHTTPTransactionCallback, public MActiveHelperCallback<RTimer*>, public MProgressDialogCallback
	{
public:
	void SetApplicationInformationL(const TDesC& aApplicationName, const TDesC8& aApplicationVersion, const TDesC8& aManifestURL, const TDesC& aDownloadPath, TInt aWaitNoteResource, TInt aInstallQueryResource, TInt aProgressNoteResource);
	void SetConnectionInfoL(const RConnection& aConnection, const RSocketServ& aSocketServ);
	void CloseConnectionL();
	void StartUpdateSequenceL(TBool aSilentUpdate, MApplicationUpdaterCallback* aTarget);
	void Cancel();
	TBool UpdatePending() const;
	static CApplicationUpdater* NewLC();
	static CApplicationUpdater* NewL();
	const TDesC& DownloadPath() const;
	virtual ~CApplicationUpdater();
private:
	RFs iFs;
	TBool iSilentUpdate;
	RHTTPSession iSession;
	HBufC* iApplicationName;
	HBufC8* iApplicationVersion;
	HBufC8* iManifestURL;
	HBufC* iDownloadPath;
	TInt iWaitNoteResource, iInstallQueryResource, iProgressNoteResource;
	void ConstructL();
	CApplicationUpdater();
	MApplicationUpdaterCallback* iTarget;
	enum { IDLE, DOWNLOAD_PHASE_MANIFEST, DOWNLOAD_PHASE_SIS } iDownloadPhase;
	CAknProgressDialog*  iDownloadProgressDialog;
	CAknWaitDialog* iDownloadWaitDialog;
	RTimer iExitTimer;
	CActiveHelper<RTimer*>* iExitTimerHelper;
	void MActiveHelperCallbackRunL(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer);
	TInt MActiveHelperCallbackRunError(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer, TInt aError);
	void MActiveHelperCallbackDoCancel(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer);
	RApaLsSession iApaLsSession;
	RHTTPTransaction iSisDownloadTransaction, iManifestTransaction;
	RBuf8 iManifest;
	TInt iLastError;
	TInt iStatusCode;
	RFile iSisDownloadFile;
    virtual void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
    virtual TInt MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
    virtual void DialogDismissedL( TInt aButtonId );
	};

#endif
